package com.hxzy.dao.impl;

import com.hxzy.dao.BooksDao;
import com.hxzy.util.DbHelper;
import entity.Books;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.BeanHandler;


import java.util.Optional;

public class BookDaoImpl extends DbHelper implements BooksDao {
    @Override
    public int insert(Books books) {
        String sql="insert into books ('name','author','time','booktype') values(?,?,?,?)";
        Object[] objects={
                books.getName(),
                books.getAuthor(),
                books.getTime(),
                books.getBookType().getId()
        };
        return super.executeUpdate(sql,objects);
    }

    @Override
    public int updateByPrimaryKey(Books books) {
        String sql="update set 'name'=?,'author'=?,'time'=?,'booktype'=? where 'id'=?";
        Object[] objects={
                books.getName(),
                books.getAuthor(),
                books.getTime(),
                books.getBookType().getId(),
                books.getId()
        };
        return super.executeUpdate(sql,objects);
    }

    @Override
    public int deleteByPrimaryKey(Integer id) {
        String sql="delete from books where 'id'=?";
        return super.executeUpdate(sql,id);
    }

    @Override
    public Optional<Books> selectByPrimaryKey(Integer id) {
        String sql="select 'id',b_name 'name',b_author 'author',b_time 'time',b_type 'bookType' where 'id'=? ";
        ResultSetHandler<Books> handler = new BeanHandler<>(Books.class);
        Optional<Books> optional = super.executeQuery(sql, handler, id);
        return optional;
    }
}
